package com.numberone.web.controller.cart;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.numberone.system.domain.*;
import com.numberone.system.service.IGoodsImgService;
import com.numberone.system.service.IGoodsService;
import com.numberone.system.service.IShopService;
import com.numberone.system.util.AssertUtils;
import com.numberone.system.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.numberone.common.annotation.Log;
import com.numberone.common.enums.BusinessType;
import com.numberone.system.service.ICartService;
import com.numberone.framework.web.base.BaseController;
import com.numberone.common.page.TableDataInfo;
import com.numberone.common.base.AjaxResult;
import com.numberone.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 购物车 信息操作处理
 * 
 * @author 于沛麟
 * @date 2020-09-29
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController extends BaseController
{
	
	@Autowired
	private ICartService cartService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IShopService shopService;
	@Autowired
	private IGoodsImgService goodsImgService;



	@PostMapping("/a")
	public Object show(Long shopId){
		return shopService.selectShopById(shopId);
	}
	/**
	 * 添加购物车
	 * @param
	 * @return
	 */
	@PostMapping("/get")
	public Object getCart(Long id,Long userId,String goodStandard,Integer num,String goodColour,Long shopId){

		Shop shop  = shopService.selectShopById(shopId);
		System.err.println(shop+"===================");
		List<GoodsImg> goodsImgs = goodsImgService.selectGoodsImgByGoodsId(id);
		GoodsImg goodsImg = goodsImgs.get(0);
		String goodUrl  = goodsImg.getUrl();
		Goods goods = goodsService.selectGoodsById(id);
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setShopId(shopId);
		cart.setShopName(shop.getName());
		cart.setGoodId(goods.getId());
		cart.setGoodName(goods.getName());
		cart.setGoodStandard(goodStandard);
		cart.setGoodPrice(goods.getPrice());
		cart.setGoodDisprice(goods.getDisPrice());
		cart.setNum(num);
		cart.setGoodColour(goodColour);
		cart.setGoodUrl(goodUrl);
		cartService.insertCart(cart);
		return JsonResult.success();
	}

	/**
	 * 删除和批量删除购物车
	 * @param ids
	 * @return
	 */
	@PostMapping("/remove")
	public Object remove(String ids)
	{
		if (ids==null){
			return "请选择要删除的商品";
		}
		int i = cartService.deleteCartByIds(ids);
		if (i > 0)
		{
			return JsonResult.success();
		}
		return JsonResult.nodata();
	}

	/**
	 * 根据用户id查看用户下面所有购物车
	 * @param
	 * @return
	 */
	@PostMapping("/list")
	public Object list( Long id){
		System.err.println(id+"============");
		List<Long> ids = new ArrayList<>();
		List<Cart> carts = cartService.selectCartByUserId(id);
		if (carts.isEmpty()){
			return JsonResult.nodata();
		}
		for (Cart cart : carts) {
			ids.add(cart.getShopId());
		}
		System.err.println(ids+"=====================");
		List<Shop> shops = shopService.selectShopByIds(ids);
		//网店铺扔购物车里面店铺id底下的商品
		for (Shop i :shops) {
			List<Cart> list = new LinkedList<>();
			for (Cart j : carts) {
				if (j.getShopId() == i.getId())
				{
					list.add(j);
				}
			}
			i.setCarts(list);
		}
		return JsonResult.success(shops);
	}
}
